#!/bin/bash
set -euo pipefail
BaseDir=$(cd "$(dirname "$0")"; pwd)
cd ${BaseDir}

mkdir -p ./pkg/{config,data,logs}

#docker-compose up -d nginx
docker-compose up -d

sleep 5
#
if [ -f ./.docker_data/initd ]; then
	echo "数据已经初始化"
else
	#这个文件从CodeAnalysis拷贝过来，微调，初始数据用
	# 这个初始化好慢，难道过程中联网还是干啥？TODO
	./_1compose_init.sh
	touch ./.docker_data/initd
	# 初始化的数据放在./.docker_da 目录下，如果想reset，删除这个目录再次运行即是全新的环境
fi

#
echo "check tca's ready status"
while true
do
	ready=`curl -s http://localhost:900/login | grep static| wc -l|| true`
	echo "::::${ready}"
	if [ ! "${ready}" = "0" ]; then
	  break
	fi
	echo "wait for tca startup..."
	sleep 10
done
echo "tca started!"

#
echo "check gitlab's ready status"
while true
do
	ready=`curl -s http://localhost:800/users/sign_in | grep gitlab| wc -l|| true`
	echo "::::${ready}"
	if [ "${ready}" = "5" ]; then
	  break
	fi
	echo "wait for gitlab startup..."
	sleep 10
done
echo "gitlab started!"

#第一次运行gitlab后密码需要记录或修改，否则这个密码文件会24小时后删除
if [ ! -f ./.docker_data/initd ]; then
	cat ./pkg/config/initial_root_password |grep 'Password: '
	# http://localhost/-/profile/password/edit 这里修改root的默认密码
fi
